﻿using Digitalmes.Core.Common.Interfaces;
using Digitalmes.Core.Common.SerialPorts;
using Digitalmes.Core.Messages;

namespace Digitalmes.Core.EventHandlers;

/// <summary>
/// 串口数据接收事件处理器。
/// </summary>
internal sealed class SerialPortDataReceivedEventHandler(IMessageQueue<SerialPortMessage> messageQueue) : INotificationHandler<SerialPortDataReceivedEvent>
{
    public async Task Handle(SerialPortDataReceivedEvent notification, CancellationToken cancellationToken)
    {
        var content = notification.Content.TrimEnd(['\r', '\n']); // 去掉尾部可能存在的回车换行符

        await messageQueue.EnqueueAsync(new SerialPortMessage(content), cancellationToken).ConfigureAwait(false);
    }
}
